package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SupplierMapper;
import com.ktjiaoyu.service.SupplierService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @return 13141
 * 2025/6/25
 * 08:46
 */
@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierMapper supplierMapper;
    // 提供setter方法注入属性
    public void setSupplierMapper(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }
    @Override
    public List<Supplier> getSupplierList() {
        return supplierMapper.getSupplierList();
    }

    @Override
    public List<Supplier> getSupplierListByName(String supName) {
        return supplierMapper.getSupplierListByName(supName);
    }

    @Override
    @Transactional
    public int saveSupplier(List<Supplier> supplierList) {
        int rows = 0;
        for (int i = 0; i < supplierList.size(); i++){
            // 拿出每一条用户数据插入
            Supplier supplier = supplierList.get(i);
            if (i == 1){
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响操作
            rows += supplierMapper.addSupplier(supplier);
        }
        // 返回总受影响操作
        return rows;
    }

    @Override
    @Transactional
    public int deleteSupplier(Integer id) {
        return supplierMapper.deleteSupplier(id);
    }

    @Override
    @Transactional
    public int updateSupplier(Supplier supplier) {
        return supplierMapper.updateSupplier(supplier);
    }
}
